package com.eam.service;

import com.eam.common.GlobalUtils;

import com.yy.base.common.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class EquipmentDataService {
	@Autowired
	private Dao dao;
	String uuid="";
	String uuids= "";
	
	
	
	//加载设备编码
	public List<Map<String,Object>> getEquipmentPlanList() {
		
		
		return dao.query("select code , name  from Emes_Facilities ");
	}
	
	//加载故障类型
	public List<Map<String,Object>> getEquipmentFaultList() {
		
		
		return dao.query("select type as type from Emes_Facilities_Fault_Log");
	}
	
	/*//加载组织机构
	public List<Map<String,Object>> getEquipmenadorgtList() {
		return dao.query("select name from ad_org where ad_orgtype_id = (select ad_orgtype_id from ad_orgtype where name = '车间')");
	}*/
	
	/*//加载组织机构下的工序
	public List<Map<String,Object>> getEquipmenprocessList(String ORGNAME) {
		return dao.query("select name from emes_process where Emes_Process_Route_id = (select Emes_Process_Route_id from Emes_Process_Route where ad_org_id =(select ad_org_id from ad_org where name =?))",ORGNAME);
	}*/
	
	/*//加载所有花篮
	public List<Map<String,Object>> getFlowerbasket() {
		return dao.query("select name , code from Emes_Facilities where Fancility_Type = 'HL' ");
	}*/
	
	//加载设备信息
	public  List<Map<String,Object>> getEquipmentInformationList(String code) {
		return dao.query("select ep.code ,ep.name ,ep.status ,fc.name as bzname , yh.name as yhname " +
				"from emes_facilities ep " +
				"left join ad_org fc on ep.ad_org_id = fc.ad_org_id " +
				"left join ad_user yh on ep.ad_user_id = yh.ad_user_id " +
				"where ep.code=?",code);
	}
	
	//查询待维修的设备
	public List<Map<String,Object>> getEquipmentcounts(String faciletiesId) {
		return dao.query("select item.emes_maintain_plan_item_id from EMES_Maintain_Plan_Item item "
				+ " left join emes_maintain_plan jh on item.emes_maintain_plan_id = jh.emes_maintain_plan_id "
				+ " where item.status in ('CREATE','SERVICE') "
				+ " and  jh.distinguish = 'REPAIR'  and item.Emes_Facilities_id = ? " , faciletiesId);
	}
	
//	//查询待维修的设备下有没有配置维修项目
//	public List<Map<String,Object>> getEquipmentprojectscounts(String faciletiesId) {
//		return dao.query("select * from Emes_Facilities_Repair where Emes_Facilities_id = ? " , faciletiesId);
//	}
	
	//查询CREATEBY和UPDATEBY的ID值
	public List<Map<String,Object>> getEquipmentputCreateby(String userIdtime){
		return dao.query("select CREATEDBY as CREATEDBY, UPDATEDBY as UPDATEDBY from ad_user where ad_user_id = ?",userIdtime);
	}
	
	//查询设备ID
	public Map<String, Object> getEquipmentfaciletiesrId(String Epcode){
		return dao.queryUnique("select emes_facilities_id  as faciletiesrId , name as SBNAME from emes_facilities    where code = ?", Epcode);
	}
	
//	//查询组织机构ID
//	public Map<String, Object> getEquipmentORGId(String orgName){
//		return dao.queryUnique("select ad_org_id  as ADORGID from ad_org  where name = ?", orgName);
//	}
	
//	//查询工序ID
//	public Map<String, Object> getEquipmentGongXuId(String gongxuNanme){
//		return dao.queryUnique("select emes_process_id as PROCESSID from emes_process where name =  ?", gongxuNanme);
//	}
	
	
	//查询故障类型ID
	public Map<String, Object> getEquipmentFaultId(String types){
		return dao.queryUnique("select Emes_Facilities_Fault_Log_id  as faultId from Emes_Facilities_Fault_Log where type = ?", types);
	}
	
	
//	//查询维修项目的责任人ID
//	public Map<String, Object> getEquipmentuserId(String userName){
//		return dao.queryUnique("select ad_user_id as userid from ad_user where name= ?", userName);
//	}
	
	//故障报修添加到设备待维修清单页面（维修开始时间选的是记录创建时间）
	@SuppressWarnings("unchecked")
	public boolean insertEmesMaintainPlanList(String plancode,Date time,String Planstatus,String adclientId,String effective,String createBy,String updatedBy,String type ,String faultId,String Remarks,String userIdtime,String adorgid) {
		boolean result = false;
		 uuid = GlobalUtils.getUUID();
		String sql =  "INSERT INTO EMES_Maintain_Plan (EMES_MAINTAIN_PLAN_ID,CODE,STATUS,AD_CLIENT_ID,ISACTIVE,CREATED,CREATEDBY,UPDATED,UPDATEDBY,DISTINGUISH,EMES_FACILITIES_FAULT_LOG_ID,REMARKS,START_DATE,AD_USER_ID,AD_ORG_ID) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		result = dao.executeSql(sql, uuid,plancode,Planstatus,adclientId,effective,time,createBy,time,updatedBy,type,faultId,Remarks,time,userIdtime,adorgid) > 0;
		
		return result;
	}
	

	
	//故障报修将设备添加到设备详情页面
	@SuppressWarnings("unchecked")
	public boolean insertEmesMaintainPlanItemList(String faciletiesId,String status,String effective,String adclientId,Date time,String createBy,String updatedBy,String adorgid) {
		boolean result = false;
		 uuids = GlobalUtils.getUUID();
		
		String sql =  "INSERT INTO EMES_MAINTAIN_PLAN_ITEM (EMES_MAINTAIN_PLAN_ITEM_ID,EMES_MAINTAIN_PLAN_ID,EMES_FACILITIES_ID,STATUS,ISACTIVE,AD_CLIENT_ID,CREATED,CREATEDBY,UPDATED,UPDATEDBY,AD_ORG_ID) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		
		result = dao.executeSql(sql, uuids,uuid,faciletiesId,status,effective,adclientId,time,createBy,time,updatedBy,adorgid) > 0;
		
		return result;
	}
	

	
}
